package Simulation;

import java.util.Vector;

import mathwork.Mathwork;
import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class ConditionLSMMulti extends ConditionLSM {
	static public Integer default_ref_numberofday=25; 
	/* This construct is for seed */
	public ConditionLSMMulti(){
		super();
	}
	public ConditionLSMMulti(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		super(ds, today, flag);
	}

	@Override
	protected Condition GetNew(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		return new ConditionLSMMulti(ds,today,flag);
	}

	@Override
	protected RawDataCell GetEstimatedValue(FxRawDataSet ds, RawDataCell today,
			EvaluatorParaFlag flag) {
		Integer numberofday=25;
		Integer ref_numberofday=ConditionLSMMulti.default_ref_numberofday;
		Integer ref_numberofday2=ConditionLSMMulti.default_ref_numberofday;
		
		if(flag.IsMFlagSet()&&flag.GetMFlagIterator().hasNext()){
			numberofday=flag.GetMFlagIterator().next();
		}
		
		if(flag.IsRFlagSet()&&flag.GetRFlagIterator().hasNext()){
			ref_numberofday=flag.GetRFlagIterator().next();
		}
		
		if(flag.IsKFlagSet()&&flag.GetKFlagIterator().hasNext()){
			ref_numberofday2=flag.GetKFlagIterator().next();
		}
		
		RawDataCell est=ds.GetLSMDataByDate(today.GetRecDate(), numberofday);
		RawDataCell est_ref1=ds.GetLSMDataByDate(today.GetRecDate(), ref_numberofday);
		RawDataCell est_ref2=ds.GetLSMDataByDate(today.GetRecDate(), ref_numberofday2);
		if(est==null){
			return null;
		}
		
		if((est_ref1==null)&&(est_ref2==null)){
			return est;
		}
		
		Vector<Double> hilist=new Vector<Double>();
		Vector<Double> lolist=new Vector<Double>();
		Vector<Double> enlist=new Vector<Double>();
		
		hilist.add(est.GetHighValue());
		lolist.add(est.GetLowValue());
		enlist.add(est.GetEndValue());
		if(est_ref1!=null){
			hilist.add(est_ref1.GetHighValue());
			lolist.add(est_ref1.GetLowValue());
			enlist.add(est_ref1.GetEndValue());
		}
		
		if(est_ref2!=null){
			hilist.add(est_ref2.GetHighValue());
			lolist.add(est_ref2.GetLowValue());
			enlist.add(est_ref2.GetEndValue());
		}
		
						
		
		return new RawDataCell(today.GetFSRecDate(),
								 today.GetStartValue(),
								 Mathwork.mean(hilist),
								 Mathwork.mean(lolist),
								 Mathwork.mean(enlist));
	}
	
}
